package com.hello.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hello.common.log.annotation.Log;
import com.hello.common.log.enums.BusinessType;
import com.hello.common.security.annotation.RequiresPermissions;
import com.hello.system.domain.RentalMemberAddresses;
import com.hello.system.service.IRentalMemberAddressesService;
import com.hello.common.core.web.controller.BaseController;
import com.hello.common.core.web.domain.AjaxResult;
import com.hello.common.core.utils.poi.ExcelUtil;
import com.hello.common.core.web.page.TableDataInfo;
import com.hello.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 会员地址Controller
 * 
 * @author hello
 * @date 2025-07-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/addresses")
public class RentalMemberAddressesController extends BaseController
{
    private final IRentalMemberAddressesService rentalMemberAddressesService;

    /**
     * 查询会员地址列表
     */
    // @RequiresPermissions("system:addresses:list")
    @GetMapping("/list")
    public TableDataInfo list(RentalMemberAddresses rentalMemberAddresses)
    {
        startPage();
        List<RentalMemberAddresses> list = rentalMemberAddressesService.selectRentalMemberAddressesList(rentalMemberAddresses);
        return getDataTable(list);
    }

    /**
     * 导出会员地址列表
     */
    // @RequiresPermissions("system:addresses:export")
    @Log(title = "会员地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RentalMemberAddresses rentalMemberAddresses)
    {
        List<RentalMemberAddresses> list = rentalMemberAddressesService.selectRentalMemberAddressesList(rentalMemberAddresses);
        ExcelUtil<RentalMemberAddresses> util = new ExcelUtil<RentalMemberAddresses>(RentalMemberAddresses.class);
        util.exportExcel(response, list, "会员地址数据");
    }

    /**
     * 获取会员地址详细信息
     */
    // @RequiresPermissions("system:addresses:query")
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") Long addressId)
    {
        return success(rentalMemberAddressesService.selectRentalMemberAddressesByAddressId(addressId));
    }

    /**
     * 新增会员地址
     */
    // @RequiresPermissions("system:addresses:add")
    @Log(title = "会员地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RentalMemberAddresses rentalMemberAddresses)
    {
        return toAjax(rentalMemberAddressesService.insertRentalMemberAddresses(rentalMemberAddresses));
    }

    /**
     * 修改会员地址
     */
    // @RequiresPermissions("system:addresses:edit")
    @Log(title = "会员地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RentalMemberAddresses rentalMemberAddresses)
    {
        return toAjax(rentalMemberAddressesService.updateRentalMemberAddresses(rentalMemberAddresses));
    }

    /**
     * 删除会员地址
     */
    // @RequiresPermissions("system:addresses:remove")
    @Log(title = "会员地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable Long[] addressIds)
    {
        return toAjax(rentalMemberAddressesService.deleteRentalMemberAddressesByAddressIds(addressIds));
    }

    /**
     * 查询当前用户的地址列表（无需权限）
     */
    @GetMapping("/my")
    public AjaxResult getMyAddresses()
    {
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            return error("用户未登录");
        }
        List<RentalMemberAddresses> list = rentalMemberAddressesService.selectRentalMemberAddressesByUserId(userId);
        return success(list);
    }
}
